package com.bw.module_home.marketer.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_home.marketer.model.BusinessProfileEntity
import com.bw.module_home.marketer.model.BusinessProfileEntityItem
import com.bw.module_home.marketer.model.MarketersRepository
import com.bw.module_home.marketer.model.ResidencyApplicationEditEntity
import com.bw.module_home.marketer.model.ResidencyApplicationEntity
import com.bw.module_home.marketer.model.ResidencyApplicationEntityItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject

@HiltViewModel
class MarketersViewModel @Inject constructor(
    private val marketersRepository: MarketersRepository
) : BaseViewModel() {

    val getBusinessProfileSuccess = MutableLiveData<BusinessProfileEntity>()
    val addBusinessProfile = MutableLiveData<BusinessProfileEntityItem>()
    val deleteBusinessProfile = MutableLiveData<List<Boolean>>()
    val getBusinessProfileDetailSuccess = MutableLiveData<BusinessProfileEntityItem>()
    val getResidencyApplicationListSuccess = MutableLiveData<ResidencyApplicationEntity>()
    val addResidencyApplicationSuccess = MutableLiveData<ResidencyApplicationEntityItem>()
    val getResidencyApplicationDetailByIdSuccess = MutableLiveData<ResidencyApplicationEntityItem>()
    val approveResidencyApplicationSuccess = MutableLiveData<ResidencyApplicationEntityItem>()
    val rejectResidencyApplicationSuccess = MutableLiveData<ResidencyApplicationEntityItem>()

    // 错误信息提示
    val mErrorLiveData = MutableLiveData<String>()

    fun getBusinessProfile() {
        marketersRepository.getBusinessProfileList()
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        getBusinessProfileSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "获取商家资料列表失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "获取商家资料列表异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }
    fun addBusinessProfile(businessProfileEntityItem: BusinessProfileEntityItem) {
        marketersRepository.addBusinessProfile(businessProfileEntityItem)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        addBusinessProfile.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "添加商家资料失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "添加商家资料异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }
    fun deleteBusinessProfile(id: Int) {
        marketersRepository.deleteBusinessProfile(id)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        // 更新 LiveData 的值
                        val currentList = deleteBusinessProfile.value?.toMutableList() ?: mutableListOf()
                        currentList.add(it)
                        deleteBusinessProfile.value = currentList
                    }
                } else {
                    Log.e("MarketersViewModel", "删除商家资料失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "删除商家资料异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }
    fun getBusinessProfileDetail(id: Int) {
        marketersRepository.getBusinessProfileDetailById(id)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        getBusinessProfileDetailSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "获取商家资料详情失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "获取商家资料详情异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }

    fun getResidencyApplicationList() {
        marketersRepository.getResidencyApplicationList()
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        getResidencyApplicationListSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "获取入驻申请列表失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "获取入驻申请列表异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }

    fun addResidencyApplication(residencyApplicationEditEntity: ResidencyApplicationEditEntity) {
        marketersRepository.addResidencyApplication(residencyApplicationEditEntity)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        addResidencyApplicationSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "添加入驻申请失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "添加入驻申请异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }

    fun getResidencyApplicationDetailById(id: Int) {
        marketersRepository.getResidencyApplicationDetailById(id)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        getResidencyApplicationDetailByIdSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "获取入驻申请详情失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "获取入驻申请详情异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }

    fun approveResidencyApplication(id: Int) {
        marketersRepository.approveResidencyApplication(id)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        approveResidencyApplicationSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "批准入驻申请失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "批准入驻申请异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }
    fun rejectResidencyApplication(id: Int) {
        marketersRepository.rejectResidencyApplication(id)
            .onEach { result ->
                if (result.code == 200) {
                    result.data?.let {
                        rejectResidencyApplicationSuccess.value = it
                    }
                } else {
                    Log.e("MarketersViewModel", "拒绝入驻申请失败  ${result.message}")
                    mErrorLiveData.value = result.message
                }
            }
            .catch { e ->
                Log.e("MarketersViewModel", "拒绝入驻申请异常: ${e.message}")
                mErrorLiveData.value = e.message.toString()
            }
            .launchIn(viewModelScope)
    }
}